/**
 * 获取同部门的其他人或其管辖部门及子部门内的人员
 */
import React from 'react';
import { Select, Spin, message } from 'antd';
import { useRequest } from '@/hooks';
import { getApplyEmployeeInfoList } from '@/services/components/userform';

const UserSelect = React.forwardRef(({ employeeCode, popupForBody, value, onChange, ...rest }, ref) => {
    // const demoData = [
    //     { employeeCode: '0a1111', employeeName: '关羽' },
    //     { employeeCode: '0a1112', employeeName: '张飞' },
    //     { employeeCode: '0a1113', employeeName: '刘备' },
    //     { employeeCode: '0a1114', employeeName: '诸葛亮' },
    //     { employeeCode: '0a1115', employeeName: '孙权' },
    //     { employeeCode: '0a1116', employeeName: '刘姥姥' },
    //     { employeeCode: '0a1117', employeeName: '张三' }
    // ];
    const [optionList, setOptionList] = React.useState([]);
    // 处理人的下拉数据改为全公司非离职人员，因为原来的范围如果本部门的其他人都离职了就没有处理人了
    const { data: sourceData = [], loading } = useRequest(getApplyEmployeeInfoList, {
        formatResult: res => {
            if (res) {
                const { code, data, msg } = res;
                if (code === 10000) {
                    setOptionList(data);
                    return data;
                }
                message.error(msg);
            }
            return undefined;
        }
    });

    const handleSearch = _value => {
        let list;
        if (_value) {
            list = sourceData.filter(item => item.employeeName.includes(_value));
        } else {
            list = sourceData;
        }
        setOptionList(list);
    };
    return (
        <Spin spinning={loading}>
            <Select
                showSearch
                value={value}
                defaultActiveFirstOption={false}
                showArrow
                filterOption={false}
                allowClear
                onSearch={handleSearch}
                onChange={onChange}
                notFoundContent={null}
                placeholder="请选择"
                getPopupContainer={triggerNode => (popupForBody ? document.body : triggerNode.parentElement)}
                ref={ref}
                {...rest}
            >
                {optionList.map(item => (
                    <Select.Option key={item.employeeCode} value={item.employeeCode}>
                        {item.employeeName}
                    </Select.Option>
                ))}
            </Select>
        </Spin>
    );
});

export default UserSelect;
